﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using DevExpress.Utils;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;

namespace Ersur.DevTeam
{
    public static class GenericExtensions
    {
        public static string ToStringNN(this object value)
        {
            string result = "";

            if (value == DBNull.Value)
            {
            }
            else if (value == null)
            {
            }
            else
            {
                result = value.ToString();
            }
            return result;
        }

        public static string ExtractValue(this string source, string caption)
        {
            return source.Substring(caption.Length, source.Length - caption.Length).Trim();
        }

        public static bool StartsWithCI(this string source,string value )
        {
            return source.ToLower().StartsWith(value.ToLower());                
        }

        public static bool IsValidEmail(this string email)
        {
            string pattern = @"^(?!\.)(""([^""\r\\]|\\[""\r\\])*""|"
              + @"([-a-z0-9!#$%&'*+/=?^_`{|}~]|(?<!\.)\.)*)(?<!\.)"
              + @"@[a-z0-9][\w\.-]*[a-z0-9]\.[a-z][a-z\.]*[a-z]$";

            Regex regex = new Regex(pattern, RegexOptions.IgnoreCase);
            return regex.IsMatch(email);
        }
    }

    public class Utility
    {
        public static decimal GetEuro(decimal dollar)
        {
            decimal euro = dollar / Settings.Parity;

            decimal result = Math.Ceiling((dollar / Settings.Parity * 10)) / 10;
            return result;
        }

        public static RepositoryItemGridLookUpEdit GetGLESablon(DataTable SourceTable, string DisplayMember, string ValueMember)
        {
            RepositoryItemGridLookUpEdit gleSablon = new RepositoryItemGridLookUpEdit();
            gleSablon.AllowNullInput = DefaultBoolean.True;
            gleSablon.View.OptionsView.ColumnAutoWidth = false;
            gleSablon.View.OptionsView.ShowAutoFilterRow = true;
            gleSablon.View.OptionsBehavior.Editable = false;
            gleSablon.View.BestFitMaxRowCount = 50;
            gleSablon.View.OptionsFind.HighlightFindResults = true;
            gleSablon.View.OptionsSelection.MultiSelectMode = GridMultiSelectMode.RowSelect;
            gleSablon.NullText = string.Empty;
            gleSablon.ImmediatePopup = true;

            gleSablon.PopupFormMinSize = new Size(500, 500);
            gleSablon.BestFitMode = DevExpress.XtraEditors.Controls.BestFitMode.BestFit;

            gleSablon.Popup += gleSablon_Popup;

            gleSablon.DataSource = SourceTable;
            gleSablon.DisplayMember = DisplayMember;
            gleSablon.ValueMember = ValueMember;

            gleSablon.PopulateViewColumns();

            foreach (GridColumn gc in gleSablon.View.Columns)
            {
                gc.FilterMode = ColumnFilterMode.DisplayText;


            }

            return gleSablon;
        }

        static void gleSablon_Popup(object sender, EventArgs e)
        {
            GridLookUpEdit gle = (GridLookUpEdit)sender;
            gle.Properties.View.BestFitColumns();
            gle.Popup -= gleSablon_Popup;
        }

        public static RepositoryItemGridLookUpEdit GetAutoCompGLE(DataTable dt, string displayMember,string valueMember)
        {
            return Utility.GetGLESablon(dt, displayMember, valueMember);
        }
    }
}